package com.example.LightningController;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;

/**
 * Created with IntelliJ IDEA.
 * User: master
 * Date: 6/15/13
 * Time: 4:32 PM
 * To change this template use File | Settings | File Templates.
 */
public class Utility {
    private static ProgressDialog dialog = null;

    public static void showAlertDialog(Context context, String msg, String title) {

        AlertDialog builder = new AlertDialog.Builder(context).create();
        builder.setMessage(msg);
        builder.setTitle(title);
//        builder.setIcon(R.drawable.icon);
        builder.setCancelable(false);
        builder.cancel();
        String ok_string = "OK";

        builder.setButton(ok_string, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                dialog.cancel();
            }
        });
        builder.show();
    }

    public static void showProgressDialog(Context context, String message) {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
        // prepare the dialog box
        dialog = new ProgressDialog(context);

        // set a message text
        dialog.setMessage(message);

        dialog.setIndeterminate(true);

        // cancel the progress bar
        dialog.setCancelable(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_SEARCH
                        && event.getRepeatCount() == 0) {
                    return true; // Pretend we processed it
                }
                return false; // Any other keys are still processed as normal
            }
        });

        // show it
        dialog.show();
    }

    public static void changeCurrentProgressDialogText(String s) {
        if(dialog != null){
            dialog.setMessage(s);
        }
    }

    public static void stopProgressDialog() {
        try {
            if (dialog != null) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
                dialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
